// 学校信息
<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <DeptOptions @accessSchool="accessSchool" />
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="100px"
        >
          <el-form-item label="学校代码" prop="schoolCode">
            <el-input
              v-model="queryParams.schoolCode"
              placeholder="请输入学校代码"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="学校名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入学校名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="办学类型" prop="schoolType">
            <el-select
              v-model="queryParams.schoolType"
              placeholder="选择办学类型"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.type_of_school"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="驻地城乡类型" prop="urbanRuralType">
            <el-select
              v-model="queryParams.urbanRuralType"
              placeholder="选择全部"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in dict.type.urban_rural_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:user:add']"
              >新增</el-button
            >
          </el-col>

          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:user:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="info"
              plain
              icon="el-icon-upload2"
              size="mini"
              @click="handleImport"
              v-hasPermi="['system:user:import']"
              >导入</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:user:export']"
              >导出</el-button
            >
          </el-col>
        </el-row>

        <el-table
          v-loading="loading"
          :data="schoolList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="学校代码"
            align="center"
            key="schoolCode"
            prop="schoolCode"
              :show-overflow-tooltip="true"
          />
          <el-table-column
            label="学校名称"
            align="center"
            key="name"
            prop="name"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="办学类型"
            align="center"
            key="schoolTypeDictText"
            prop="schoolTypeDictText"
          />

          <el-table-column
            label="在校总人数"
            align="center"
            key="personCount"
            prop="personCount"
            width="120"
          />

          <el-table-column
            label="校长"
            align="center"
            key="legalPerson"
            prop="legalPerson"
          />

          <el-table-column
            label="驻地城乡类型"
            align="center"
            key="urbanRuralTypeDictText"
            prop="urbanRuralTypeDictText"
            width="120"
          />
          <el-table-column
            label="行政区划"
            align="center"
            key="orgName"
            prop="orgName"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit-outline"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:user:edit']"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-share"
                @click="handleUpdate(scope.row, '详情')"
                v-hasPermi="['system:user:remove']"
                >详情</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:user:remove']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改学生信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-row>
          <el-col :span="12">
            <el-form-item label="学校标识码" prop="schoolCode">
              <el-input
                v-model="form.schoolCode"
                placeholder="请输入学校标识码"
                :disabled="displays"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学校名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入学校名称"
                :disabled="displays"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="办学类型" prop="schoolType">
              <el-select
                v-model="form.schoolType"
                placeholder="选择办学类型"
                clearable
                style="width: 230px"
                :disabled="displays"
              >
                <el-option
                  v-for="dict in dict.type.type_of_school"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="驻地城乡类型"
              prop="urbanRuralType"
              label-width="130px"
              :disabled="displays"
            >
              <el-select
                v-model="form.urbanRuralType"
                placeholder="请选择驻地城乡类型"
                clearable
                style="width: 200px"
              >
                <el-option
                  v-for="item in dict.type.urban_rural_type"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="学校地址" prop="address">
              <el-input
                v-model="form.address"
                placeholder="请输入学校地址"
                :disabled="displays"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="在校总人数" prop="personCount">
              <el-input
                v-model="form.personCount"
                placeholder="请输入在校人数"
                :disabled="displays"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="行政区划" prop="deptCode">
              <el-cascader
                :options="deptOptions"
                v-model="form.deptCode"
                @change="handleChange"
                style="width: 240px"
                :disabled="displays"
                :props="{ checkStrictly: true }"
                clearable
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="校长姓名" prop="legalPerson">
              <el-input
                v-model="form.legalPerson"
                placeholder="请输入校长姓名"
                :disabled="displays"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="电 话" prop="phoneNum">
              <el-input
                v-model="form.phoneNum"
                placeholder="请输入联系方式"
                :disabled="displays"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学 制" prop="schoolYear">
              <el-input
                type="number"
                v-model="form.schoolYear"
                placeholder="请输入学制"
                :disabled="displays"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="经 度" prop="longitude">
              <el-input
                v-model="form.longitude"
                placeholder="请输入经度"
                :disabled="displays"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬 度" prop="latitude">
              <el-input
                v-model="form.latitude"
                placeholder="请输入纬度"
                :disabled="displays"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="简介">
              <el-input
                v-model="form.introduction"
                type="textarea"
                placeholder="请输入简介"
                :disabled="displays"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                :disabled="displays"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!displays"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" />
            是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate"
            >下载模板</el-link
          >
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import DeptOptions from "@/components/DeptOptions";
import { deptTreeSelect } from "@/api/system/user";
import {
  schoolAdd, //新增学校
  schoolAlter, //修改学校
  schoolPage, //获取学校
  schoolDel, //删除学校
} from "@/api/information/index";
import { getToken } from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "User",
  dicts: [
    "sys_normal_disable",
    "sys_user_sex",
    "sys_label_type",
    "type_of_school",
    "urban_rural_type",
  ],
  components: { Treeselect, DeptOptions },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      shollName: [],
      displays: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      schoolList: null,
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 默认密码
      initPassword: undefined,
      // 日期范围
      dateRange: [],
      // 岗位选项
      postOptions: [],
      // 角色选项
      roleOptions: [],
      // 表单参数
      form: {
        type: 2, //学校接口
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/user/importData",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: 2, //学校接口
      },
      // 学校信息
      schoolLiat: [
        { value: 0, label: `吴忠市第一小学` },
        { value: 1, label: `吴忠市第二小学` },
        { value: 2, label: `吴忠市第三小学` },
        { value: 3, label: `吴忠市第四小学` },
        { value: 4, label: `吴忠市第五小学` },
        { value: 5, label: `吴忠市第六小学` },
        { value: 6, label: `吴忠市第七小学` },
      ],
      //   班级
      classlLiat: [
        { value: 0, label: `一班` },
        { value: 1, label: `二班` },
        { value: 2, label: `三班` },
        { value: 3, label: `四班` },
        { value: 4, label: `五班` },
        { value: 5, label: `六班` },
        { value: 6, label: `七班` },
      ],
      // 名族
      politicsLiat: [
        { value: 1, label: `中共党员` },
        { value: 2, label: `群众` },
        { value: 3, label: `民主党派人士` },
      ],
      //   性别
      genderLiat: [
        { value: 1, label: `男` },
        { value: 2, label: `女` },
      ],
      //   标签
      labelLiat: [
        { value: 1, label: `重度` },
        { value: 2, label: `中度` },
        { value: 3, label: `轻度` },
        { value: 4, label: `正常` },
      ],
      // 表单校验
      rules: {
        schoolCode: [
          { required: true, message: "学校编码不能为空", trigger: "blur" },
          {
            pattern: /^[0-9a-zA-Z]+$/,
            message: "格式错误，仅支持英文和数字",
            trigger: "blur",
          },
        ],
        name: [
          { required: true, message: "学校名称不能为空", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "名称长度必须介于 2 和 20 之间",
            trigger: "blur",
          },
        ],

        schoolType: [
          { required: true, message: "请选择办学类型", trigger: "blur" },
        ],
        deptCode: [{ required: true, message: "请选择行政区划", trigger: "blur" }],
        phoneNum: [
          { required: true, message: "联系方式不能为空", trigger: "blur" },
          // {
          //   pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
          //   message: "请输入正确的手机号码",
          //   trigger: "blur",
          // },
        ],
      },
    };
  },
  watch: {},
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = JSON.parse(
          JSON.stringify(response.data).replace(/id/g, "value")
        );
      });
    },
    /** 查询学生信息列表 */
    getList() {
      this.loading = true;
      schoolPage(this.queryParams).then((res) => {
        console.log("获取学校", res);
        this.schoolList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    handleChange(value) {
      if (value) {
           this.form.deptCode =value[value.length - 1];
        }
    },

    accessSchool(data) {
           this.queryParams.pageNum = 1;
           this.queryParams.deptCode = data.deptCode;
      this.getList();
      console.log("学校信息", data);
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        type: "2",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
 this.queryParams.deptCode =null
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.shollName = selection.map((item) => item.name);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新建学校";
    },
    /** 修改按钮操作 */
    handleUpdate(row, val) {
      if (val === "详情") {
        this.displays = true;
        this.title = "学校详情";
      } else {
        this.displays = false;
        this.title = "编辑学校";
      }
      this.reset();
      console.log("编辑学校",row)
      this.form = row;
      this.open = true;
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            schoolAlter(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            schoolAdd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.id || this.ids;
      const shollnmae = row.name || this.shollName;
      this.$modal
        .confirm('是否确认删除"' + shollnmae + '"的数据项？')
        .then(function () {
          return schoolDel(userIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/user/export",
        {
          ...this.queryParams,
        },
        `user_${new Date().getTime()}.xlsx`
      );
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "system/user/importTemplate",
        {},
        `user_template_${new Date().getTime()}.xlsx`
      );
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  },
};
</script>
<style lang="scss" scoped>
.labelys {
  width: 100%;
  height: 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  div:nth-child(1) {
    width: 10px;
    height: 10px;
    background-color: red;
  }
}
</style>